package net.obviam.droidz;

import net.obviam.droidz.model.Droid;
import net.obviam.droidz.model.components.Speed;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainGamePanel extends SurfaceView implements
		SurfaceHolder.Callback {

	private static final String TAG = MainGamePanel.class.getSimpleName();

	private MainThread thread;
	private Droid droid;
	
	//private int newX, newY;
	private float newX, newY;
	private int sp=3;

	public MainGamePanel(Context context) {
		super(context);
		// adding the callback (this) to the surface holder to intercept events
		getHolder().addCallback(this);

		// create droid and load bitmap
		droid = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.droid_1), getHeight()-50, 50);
		
		// create the game loop thread
		thread = new MainThread(getHolder(), this);

		// make the GamePanel focusable so it can handle events
		setFocusable(true);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// at this point the surface is created and
		// we can safely start the game loop
		thread.setRunning(true);
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "Surface is being destroyed");
		// tell the thread to shut down and wait for it to finish
		// this is a clean shutdown
		boolean retry = true;
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				// try again shutting down the thread
			}
		}
		Log.d(TAG, "Thread was shut down cleanly");
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			// delegating event handling to the droid
			droid.handleActionDown((int)event.getX(), (int)event.getY());

			// check if in the lower part of the screen we exit
			if (event.getY() > getHeight() - 50) {
				thread.setRunning(false);
				((Activity)getContext()).finish();
				
				
			} else {
				//starts x,y moving by gustavo
				
				newX=event.getX();
				newY=event.getY();
				
				//xy_move();
				plain_move();
				
				
				//Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
				Log.d(TAG, "Coords: x=" + newX + ",y=" + newY);
			
				

				
			}
		} if (event.getAction() == MotionEvent.ACTION_MOVE) {
			// the gestures
			if (droid.isTouched()) {
				// the droid was picked up and is being dragged
				droid.setX((int)event.getX());
				droid.setY((int)event.getY());
			}
		} if (event.getAction() == MotionEvent.ACTION_UP) {
			// touch was released
			if (droid.isTouched()) {
				droid.setTouched(false);
			}
		}
		return true;
	}

	
	// the fps to be displayed
	private String avgFps;
	public void setAvgFps(String avgFps) {
		this.avgFps = avgFps;
	}

	public void render(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		droid.draw(canvas);
		// display fps
		displayFps(canvas, avgFps);
	}

	private void displayFps(Canvas canvas, String fps) {
		if (canvas != null && fps != null) {
			Paint paint = new Paint();
			paint.setARGB(255, 255, 255, 255);
			canvas.drawText(fps, this.getWidth() - 50, 20, paint);
		}
	}
	public void update() {
		// check collision with right wall if heading right
		if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT
				&& droid.getX() + droid.getBitmap().getWidth() / 2 >= getWidth()) {
			droid.getSpeed().toggleXDirection();
		}
		// check collision with left wall if heading left
		if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT
				&& droid.getX() - droid.getBitmap().getWidth() / 2 <= 0) {
			droid.getSpeed().toggleXDirection();
		}
		// check collision with bottom wall if heading down
		if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN
				&& droid.getY() + droid.getBitmap().getHeight() / 2 >= getHeight()) {
			droid.getSpeed().toggleYDirection();
		}
		// check collision with top wall if heading up
		if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP
				&& droid.getY() - droid.getBitmap().getHeight() / 2 <= 0) {
			droid.getSpeed().toggleYDirection();
		}
		
		//xy_update();
		plain_update();
		
		
		// Update the lone droid
		droid.update();
	}
	public void xy_move(){
		droid.setSpeed(new Speed(2,0));
		
		if(newX < droid.getX()){
			//droid.getSpeed().setxDirection(Speed.DIRECTION_LEFT);
			droid.getSpeed().toggleXDirection();
		}
	}
	public void xy_update(){
		//starts x,y moving by gustavo
		if(droid.getX()<newX+2 && droid.getX()>newX-2){
			droid.setSpeed(new Speed(0,2));
			if(newY < droid.getY()){
				droid.getSpeed().toggleYDirection();
			}
				}
			if(droid.getY()<newY+2 && droid.getY()>newY-2){
				droid.setSpeed(new Speed(0,0));
			}//ends x,y moving
	}
	public void plain_move(){
		
		float dx=droid.getX();
		float dy=droid.getY();
		Log.d(TAG, "dx=" + dx + ",dy=" + dy);
		float difX=newX-dx;
		float difY=newY-dy;
		//float dif_YX=difY/difX;
		//float dif_XY=difX/difY;
		Log.d(TAG, "difx=" + difX + ",dify=" + difY);
		float d_xy=(float) Math.sqrt(Math.pow((difX),2)+Math.pow((difY),2));
		Log.d(TAG, "d_xy=" + d_xy);
		float vx=difX/d_xy;
		float vy=difY/d_xy;
		//float vx=d_xy/50;
		//float vy=d_xy/50;
		//float vx=sp*(difX/d_xy);
		//float vy=vx*(difY/difX);
		//float vx=3;
		//float vy=vx*(difY/difX);
		Log.d(TAG, "vx="+vx+",vy="+vy);
		
		droid.setSpeed(new Speed((float)(vx),(float)(vy)));
		
		//if(newX < droid.getX()){
			//droid.getSpeed().setxDirection(Speed.DIRECTION_LEFT);
			//droid.getSpeed().toggleXDirection();
		//}
		//if(newY < droid.getY()){
			//droid.getSpeed().setxDirection(Speed.DIRECTION_LEFT);
			//droid.getSpeed().toggleYDirection();
		//}
	}
	public void plain_update(){
		int dx=droid.getX();
		int dy=droid.getY();
		if(dx<newX+sp+sp && dx>newX-sp-sp && 
				dy<newY+sp+sp && dy>newY-sp+sp){
			droid.setSpeed(new Speed(0,0));
		}
	}

}
